{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Trading Context\n",
    "\n",
    "In this notebook we will explain how to use a TensorTrade `TradingContext`. In addition we will also explain the backbone behind TensorTrade's major components and how they are used inside the framework. We will begin with an easy example of how to use the most basic context, this involves specifying what `base_intrument` we are using and what `instruments` we are exchanging. By default, the framework sets these as `USD` and `BTC`, however, we would like to specify our instruments as `BTC` and `ETH`. We also don't want to have to put them as parameters to every component we make because we know it will be the same for all components. Therefore, we use a trading context to set these parameters across the entire framework."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensortrade as td\n",
    "\n",
    "from tensortrade.environments import TradingEnvironment\n",
    "\n",
    "config = {\n",
    "    \"base_instrument\": \"USD\",\n",
    "    \"instruments\": [\"BTC\", \"ETH\"]\n",
    "}\n",
    "\n",
    "with td.TradingContext(**config):\n",
    "    \n",
    "    env = TradingEnvironment(exchange='simulated',\n",
    "                             action_scheme='discrete',\n",
    "                             reward_scheme='simple')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Special Keys\n",
    "\n",
    "The following keys are considered special configuration keys:\n",
    "  *  `shared`\n",
    "  *  `exchanges`\n",
    "  *  `actions`\n",
    "  *  `rewards`\n",
    "  *  `features`\n",
    "  *  `slippage`\n",
    "  \n",
    "There is one for each major component of the library. This ensures that each component gets the parameters it needs. The ones that get shared across components get stored on either the top level of this dictionary or inside the `shared` special key. After the creating the configuration dictionary, all that needs to be done is to use a `TradingContext` in a `with` statement. Any components that are defined under the `with` statement are injected with shared parameters as well as the parameters for the special configuration corresponding to the component that has been initialized."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initializing a Trading Environment from a Configuration\n",
    "\n",
    "For example, this configuration sets up a trading environment using a `DiscreteActions` with 24 actions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = {\n",
    "    \"base_instrument\": \"USD\",\n",
    "    \"instruments\": [\"BTC\", \"ETH\"],\n",
    "    \"actions\": {\n",
    "        \"n_actions\": 24\n",
    "    }\n",
    "}\n",
    "\n",
    "with TradingContext(**config):\n",
    "    \n",
    "    env = TradingEnvironment(exchange='simulated',\n",
    "                         action_scheme='discrete',\n",
    "                         reward_scheme='simple')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = {\n",
    "    \"base_instrument\": \"USD\",\n",
    "    \"instruments\": [\"BTC\", \"ETH\"],\n",
    "    \"actions\": {\n",
    "        \"n_actions_per_instrument\": 25,\n",
    "        'max_allowed_slippage_percent': 5.0\n",
    "    }\n",
    "}\n",
    "\n",
    "with TradingContext(**config):\n",
    "    \n",
    "    action_scheme = MultiDiscreteActions()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize Live Trading Environment with Credentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = {\n",
    "    \"base_instrument\": \"USD\",\n",
    "    \"instruments\": [\"BTC\", \"ETH\"],\n",
    "    \"actions\": {\n",
    "        \"n_actions\": 25,\n",
    "        'max_allowed_slippage_percent': 5.0\n",
    "    },\n",
    "    \"exchanges\": {\n",
    "        \"credentials\": {\n",
    "            \"key\": \"o3874hfowiulejhrbf\",\n",
    "            \"b64secret\": \"fo4hruwvoliejrbvwrl\",\n",
    "            \"passphrase\": \"f9ohr8oviu3rbvlufb3iuebfo\"\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "with TradingContext(**config):\n",
    "    \n",
    "    env = TradingEnvironment(exchange='coinbase',\n",
    "                         action_scheme='discrete',\n",
    "                         reward_scheme='simple')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = {\n",
    "    \"base_instrument\": \"USD\",\n",
    "    \"instruments\": [\"BTC\", \"ETH\"],\n",
    "    \"actions\": {\n",
    "        \"n_actions\": 25,\n",
    "        'max_allowed_slippage_percent': 5.0\n",
    "    }\n",
    "}\n",
    "\n",
    "exchange = StochasticExchange(base_price=2, base_volume=2)\n",
    "\n",
    "with TradingContext(**config):\n",
    "    \n",
    "    env = TradingEnvironment(exchange=exchange,\n",
    "                         action_scheme='discrete',\n",
    "                         reward_scheme='simple')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Initialize from File\n",
    "\n",
    "The two file format that are currently supported for creating trading context are JSON and YAML. The following are examples of what these files might look like for customization of action scheme and exchange parameters.\n",
    "\n",
    "### YAML\n",
    "```YAML\n",
    "base_instrument: \"EURO\"\n",
    "instruments: [\"BTC\", \"ETH\"]\n",
    "actions:\n",
    "  n_actions: 24\n",
    "  max_allowed_slippage_percent: 5.0\n",
    "exchanges:\n",
    "  credentials:\n",
    "    api_key: \"487r63835t4323\"\n",
    "    api_secret_key: \"do8u43hgiurwfnlveio\"\n",
    "    min_trade_price: 1e-7\n",
    "    max_trade_price: 1e7\n",
    "    min_trade_size: 1e-4\n",
    "    max_trade_size: 1e4\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "### JSON\n",
    "```JSON\n",
    "{\n",
    "    \"base_instrument\": \"EURO\",\n",
    "    \"instruments\": [\"BTC\", \"ETH\"],\n",
    "    \"exchanges\": {\n",
    "      \"commission_percent\": 0.5,\n",
    "      \"base_precision\": 0.3,\n",
    "      \"instrument_precision\": 10,\n",
    "      \"min_trade_price\": 1e-7,\n",
    "      \"max_trade_price\": 1e7,\n",
    "      \"min_trade_size\": 1e-4,\n",
    "      \"max_trade_size\": 1e4,\n",
    "      \"initial_balance\": 1e5,\n",
    "      \"window_size\": 5,\n",
    "      \"should_pretransform_obs\": true,\n",
    "      \"max_allowed_slippage_percent\": 3.0,\n",
    "      \"slippage_model\": \"uniform\"\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yaml_path = \"data/config/configuration.yaml\"\n",
    "json_path = \"data/config/configuration.json\"\n",
    "\n",
    "with TradingContext.from_yaml(yaml_path):\n",
    "    \n",
    "    env = TradingEnvironment(exchange='fbm',\n",
    "                         action_scheme='discrete',\n",
    "                         reward_scheme='simple')\n",
    "    \n",
    "with TradingContext.from_json(json_path):\n",
    "    \n",
    "    env = TradingEnvironment(exchange='fbm',\n",
    "                         action_scheme='discrete',\n",
    "                         reward_scheme='simple')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
